package com.jackrain.nea.omslocal.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.util.TypeUtils;
import com.jackrain.nea.omslocal.Dao.TaobaoOrderDao;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.Tools;
import com.jackrain.nea.web.query.QueryEngine;
import lombok.Synchronized;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Discription: 保存线上同步库存错误条码
 * Created with IntelliJ IDEA.
 * @Author cs
 * @Date 2017/9/21 9:21
 */
@Component
@Slf4j
public class SaveSkuErrservice {

    /**
     * 保存线上同步库存错误条码
     *
     * @param jsonArray
     */
    @Synchronized
    public void saveSkuerr(JSONArray jsonArray) {

        SaveSkuErrservice saveSkuErrservice = ApplicationContextHandle.getBean(SaveSkuErrservice.class);
        if (!jsonArray.isEmpty()) {
            // 新增数据
            String insertSql = "insert into t_iessynerr (ID,PLATFORM,NUM_IID,SKU,OUTSKUID,SELLER_NICK,PROPERTIES,ERRMSG) values(?,?,?,?,?,?,?,?) ";
            // 获取squ
            String squnceSql = "select get_wsequences('t_iessynerr') as SEQ from dual";
            boolean isSuccess = true;
            for (Object obj : jsonArray) {

                JSONObject tempJsonObj = JSONObject.parseObject(obj.toString(), Feature.OrderedField);
                log.debug(tempJsonObj.toString());
                int platform = tempJsonObj.getIntValue("PLATFORM");
                try {
                    isSuccess = tempJsonObj.getBoolean("isSuccess");

                } catch (NullPointerException e) {
                    //  空指针, 没有isSuccess 参数,默认同步失败
                    isSuccess =false;
                }

                if (isSuccess) {
                    // 成功
                    updateQTYStatus(tempJsonObj, isSuccess);
                    // 根据sku更新t_iessku表safenum值
                    if(platform == 19){
                        try{
                            //库存占用
                            Integer current_hold = tempJsonObj.getInteger("CURRENT_HOLD");
                            String skugb = tempJsonObj.getString("SKUGB");
                            String sql = "update T_IESSKU set SAFENUM = ? where SKU = ?";
                            if(current_hold >= 0){
                                QueryEngine.getInstance().executeUpdate(sql, new Object[]{current_hold, skugb});
                            }
                        }catch (Exception e){
                            e.printStackTrace();
                            continue;
                        }
                    }
                    continue;
                }
                // 获取id
                int id = TypeUtils.castToInt(QueryEngine.getInstance().doQueryOne(squnceSql));

                String numiid = tempJsonObj.getString("NUM_IID");
                String sku = tempJsonObj.getString("SKU");
                String outskuid = tempJsonObj.getString("OUTSKUID");
                String sellernick = tempJsonObj.getString("SELLER_NICK");
                String properties = tempJsonObj.getString("PROPERTIES");
                String errmsg = tempJsonObj.getString("ERRMSG");
                int code = QueryEngine.getInstance().executeUpdate(insertSql, new Object[]{id, platform, numiid, sku, outskuid, sellernick, properties, errmsg});
                // 修改中间表同步状态
                updateQTYStatus(tempJsonObj, isSuccess);
            }


        }
    }

    /**
     * 根据传入错误信息修改库存同步中间表的状态
     */
    public void updateQTYStatus(JSONObject jsonObject, boolean isSuccess) {

        // 0 待更新  1 已发送  2 超时  3 更新失败 , 4 成功
        String updateSql = "update T_IESSYNSTOCKMQ set synstatus = ? ,errormsg = ? where id = ? ";

        long id = jsonObject.getLong("ID");
        String errMsg = jsonObject.getString("ERRMSG");
        if (isSuccess) {
            // 成功
            QueryEngine.getInstance().executeUpdate(updateSql, new Object[]{4, errMsg, id});

        } else {
            // 失败
            boolean isTimeOut = false;
            try {
                isTimeOut = jsonObject.getBoolean("isTimeOut");
            } catch (NullPointerException e) {
                //  空指针, 没有isSuccess 参数,默认同步失败
                isTimeOut = false;
            }

            if(isTimeOut){
                QueryEngine.getInstance().executeUpdate(updateSql, new Object[]{2, errMsg, id});
            }else {
                QueryEngine.getInstance().executeUpdate(updateSql, new Object[]{3, errMsg, id});
            }
        }

    }


}
